package com.huichenghe.xinlvsh01.Ble;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;

import com.huichenghe.xinlvsh01.Utils.LocalDataSaveTool;
import com.huichenghe.xinlvsh01.Utils.MyConfingInfo;

/**
 * 获取手环版本号
 * Created by lixiaoning on 15-11-10.
 */
public class BleDataForHardVersion extends BleBaseDataManage
{
    public final static byte send_cmd = (byte)0x07;
    public final static byte back_cmd = (byte)0x87;

    private static String versionString = null;     // 储存版本字符串的变量

    public BleDataForHardVersion() {
        setMessageDataByString(send_cmd, null, true);
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        setMessageDataByString(send_cmd, null, true);
    }


    /**
     * 处理手环接收到的数据域数据
     * @param mContext  上下文
     * @param bufferTmp byte数组
     * @param dataLen   数据长度
     */
    public static void dealReceData(Context mContext, byte[] bufferTmp, int dataLen)
    {
        byte hard = bufferTmp[0];
        byte bluetooth = bufferTmp[1];
        byte soft = bufferTmp[2];

        versionString = parseTheHexString(hard) + "/"
                      + parseTheHexString(soft) + "/"
                      + parseTheHexString(bluetooth);
        /**
         * 写入sp
         */
        LocalDataSaveTool.getInstance(mContext).writeSp(MyConfingInfo.HARD_VERSION, versionString);




    }


    public static String getVersionString()
    {
        return versionString;
    }


    /**
     * 按协议解析版本号
     * @param hard
     */
    private static String parseTheHexString(byte hard)
    {
        byte a = (byte)(hard >> 4);
        byte b = (byte)(hard & 0x0f);

        return a + "" + b;
    }
}
